package com.syni.mdd.sjb.common.datatreaure.service.impl.databank;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DankMerchantsDefBusiness;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppIndustry;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppUserChannel;
import com.syni.mdd.sjb.common.component.utils.common.utils.*;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiBusiness.GetBusinessDataByIdDto;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiBusiness.GetBusinessDataByIdsDto;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiBusiness.GetUserClaimBusinessRpDto;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiOrder.GetByBusinessOrderForDataBankDto;
import com.syni.mdd.sjb.common.component.utils.feign.service.mdd.sjb.common.datatreasure.MinAppOrderService;
import com.syni.mdd.sjb.common.component.utils.feign.service.mddAll.BusinessAuthRestService;
import com.syni.mdd.sjb.common.component.utils.feign.service.mddAll.BusinessRestService;
import com.syni.mdd.sjb.common.component.utils.feign.service.mddAll.OrderRestService;
import com.syni.mdd.sjb.common.datatreaure.service.*;
import com.syni.mdd.sjb.common.datatreaure.service.databank.DataBankBusinessService;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultCode;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.datatreaure.controller.protect.dataTreasure.business.BankBusinessController;
import com.syni.mdd.sjb.common.datatreaure.entity.bo.dataBankBusiness.BankBusinessBo;
import com.syni.mdd.sjb.common.datatreaure.entity.bo.dataBankBusiness.CdpUtilsBo;
import com.syni.mdd.sjb.common.datatreaure.entity.bo.dataBankBusiness.OrderDeliveryTypeTotalBo;
import com.syni.mdd.sjb.common.datatreaure.entity.bo.dataBankBusiness.SaleGoodTableDataBo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.dataBankBusiness.*;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.userChannel.GetMyAllChannelRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.userChannel.GetMyStoreRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.userChannel.ManagementHomePageRpVo_v2;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.databank.DataBankBusinessVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.databank.MinAppOrdersRqVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.databank.UnifiedCodeQueryVo;
import com.syni.mdd.sjb.common.datatreaure.utils.CDPSimDataUtils;
import com.syni.mdd.sjb.common.datatreaure.utils.CDPUtils;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class DataBankBusinessServiceImpl implements DataBankBusinessService {

    private final Logger log = LoggerFactory.getLogger(getClass());

    final static int STAT_HOUR = 1;
    final static int STAT_WEEK = 2;
    final static int STAT_THIRD = 3;
    final static int STAT_MONTH = 4;

    @Autowired
    BusinessAuthRestService businessAuthRestService;

    @Autowired
    BusinessRestService businessRestService;

    @Autowired
    DankMerchantsDefBusinessService dankMerchantsDefBusinessService;

    @Autowired
    DxshAppUserChannelService dxshAppUserChannelService;

    @Autowired
    OrderRestService orderRestService;

    @Autowired
    MinAppOrderService minAppOrderService;

    @Autowired
    UserService userService;

    @Autowired
    DankCdpOrderTotalService dankCdpOrderTotalService;

    @Autowired
    DxshAppIndustryChooseService dxshAppIndustryChooseService;


    public static Map<Integer, String> orderTypeMap = new HashMap<>();

    /*
    堂食+外卖小程序：待付款0dfk、待处理7dcl、待配送1dfh、-待收货2yfh、已完成5ywc（标注堂食or外卖）
    商城小程序：     待付款0dfk、待处理1dcl（即未发货）、待收货yfh（即骑手已取货/已填写快递单号）2、已完成5ywc
     */
    static {
        orderTypeMap.put(-1, "yqc");
        orderTypeMap.put(0, "dfk");
        orderTypeMap.put(5, "ywc");
        orderTypeMap.put(1, "dfh");
        orderTypeMap.put(2, "yfh");
        orderTypeMap.put(7, "dcl");
        orderTypeMap.put(8, "dzt");
    }


    @Override
    public Map<String, Object> searchAlreadyBusiness(DataBankBusinessVo dataBankBusinessVo) {
        SearchAlreadyBusinessRpVo searchAlreadyBusinessRpVo = new SearchAlreadyBusinessRpVo();
        RpcResponseVo rpcResponseVo = businessRestService.getUserClaimBusiness(dataBankBusinessVo.getUserId(), Constants.COMMON_FLAG_FALSE_INT);
        if (rpcResponseVo.isSuccess()) {
            List<GetUserClaimBusinessRpDto> getUserClaimBusinessRpDtos = rpcResponseVo.parseArray(GetUserClaimBusinessRpDto.class);
            if (CollectionUtils.isNotEmpty(getUserClaimBusinessRpDtos)) {
                for (GetUserClaimBusinessRpDto getUserClaimBusinessRpDto : getUserClaimBusinessRpDtos) {
                    SearchAlreadyBusinessRpVo.ClaimBusiness claimBusiness = new SearchAlreadyBusinessRpVo.ClaimBusiness();
                    claimBusiness.setBusinessId(getUserClaimBusinessRpDto.getBusinessId());
                    claimBusiness.setVendorName(getUserClaimBusinessRpDto.getVendorName());
                    if (BankBusinessController.industryTypeMap.containsKey(getUserClaimBusinessRpDto.getTypeName())) {
                        claimBusiness.setTypeName(BankBusinessController.industryTypeMap.get(getUserClaimBusinessRpDto.getTypeName()));
                    }
                    searchAlreadyBusinessRpVo.getClaimBusinesses().add(claimBusiness);
                }
            }
        }
        return ResultMapHelper.success(searchAlreadyBusinessRpVo, ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    public boolean chooseDefaultBusiness(Integer merchantsUserId, Integer businessId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DankMerchantsDefBusiness.USER_ID, merchantsUserId);
        List<DankMerchantsDefBusiness> dankMerchantsDefBusinesses = dankMerchantsDefBusinessService.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(dankMerchantsDefBusinesses)) {
            DankMerchantsDefBusiness dankMerchantsDefBusiness = dankMerchantsDefBusinesses.get(0);
            dankMerchantsDefBusiness.setBusinessId(businessId);
            return dankMerchantsDefBusinessService.updateById(dankMerchantsDefBusiness);
        }
        DankMerchantsDefBusiness dankMerchantsDefBusiness = new DankMerchantsDefBusiness();
        dankMerchantsDefBusiness.setUserId(merchantsUserId);
        dankMerchantsDefBusiness.setBusinessId(businessId);
        return dankMerchantsDefBusinessService.save(dankMerchantsDefBusiness);
    }

    @Override
    public BankBusinessBo getChooseBusinessData(Integer businessId) {
        GetBusinessDataByIdsDto getBusinessDataByIdsDto = new GetBusinessDataByIdsDto();
        Set<Integer> businessIds = new HashSet<>();
        businessIds.add(businessId);
        getBusinessDataByIdsDto.setBusinessIds(businessIds);
        RpcResponseVo rpcResponseVo = businessRestService.getBusinessByIds(getBusinessDataByIdsDto);
        if (!rpcResponseVo.isSuccess()) {
            return null;
        }
        getBusinessDataByIdsDto = rpcResponseVo.parseObject(GetBusinessDataByIdsDto.class);
        List<GetBusinessDataByIdDto> businessData = getBusinessDataByIdsDto.getBusinessData();
        if (!CollectionUtils.isNotEmpty(businessData)) {
            return null;
        }
        BankBusinessBo bankBusinessBo = new BankBusinessBo();
        bankBusinessBo.setBusinessId(businessData.get(0).getId());
        bankBusinessBo.setTypeName(businessData.get(0).getTags());
        bankBusinessBo.setVendorName(businessData.get(0).getVendorName());
        return bankBusinessBo;
    }

    @Override
    public Map<String, Object> marketMddOrders(UnifiedCodeQueryVo unifiedCodeQueryVo) {
        MarketMddOrdersRpVo marketMddOrdersRpVo = new MarketMddOrdersRpVo();
        marketMddOrdersRpVo.setPageNum(unifiedCodeQueryVo.getPageNum());
        marketMddOrdersRpVo.setPageSize(unifiedCodeQueryVo.getPageSize());
        DxshAppUserChannel dxshAppUserChannel = dxshAppUserChannelService.getById(unifiedCodeQueryVo.getChannelId());
        if (dxshAppUserChannel == null) {
            return ResultMapHelper.success(marketMddOrdersRpVo, ResultMsg.MSG_GET_SUCCESS);
        }
        Integer businessId = dxshAppUserChannel.getBusinessId();
        GetByBusinessOrderForDataBankDto getByBusinessOrderForDataBankDto = new GetByBusinessOrderForDataBankDto();
        getByBusinessOrderForDataBankDto.setBusinessId(businessId);
        getByBusinessOrderForDataBankDto.setPageNum(unifiedCodeQueryVo.getPageNum());
        getByBusinessOrderForDataBankDto.setPageSize(unifiedCodeQueryVo.getPageSize());
        getByBusinessOrderForDataBankDto.setOrderType(unifiedCodeQueryVo.getOrderType());
        RpcResponseVo rpcResponseVo = orderRestService.getByBusinessOrderForDataBank(getByBusinessOrderForDataBankDto);
        if (!rpcResponseVo.isSuccess()) {
            return ResultMapHelper.success(marketMddOrdersRpVo, ResultMsg.MSG_GET_SUCCESS);
        }
        getByBusinessOrderForDataBankDto = rpcResponseVo.parseObject(GetByBusinessOrderForDataBankDto.class);
        List<GetByBusinessOrderForDataBankDto.MddOrderData> mddOrderData = getByBusinessOrderForDataBankDto.getOrders();
        if (CollectionUtils.isNotEmpty(mddOrderData)) {
            for (GetByBusinessOrderForDataBankDto.MddOrderData md : mddOrderData) {
                MarketMddOrdersRpVo.MarketMddOrdersRpMddOrder marketMddOrdersRpMddOrder = new MarketMddOrdersRpVo.MarketMddOrdersRpMddOrder();
                marketMddOrdersRpMddOrder.setOrderType(md.getOrderType());
                if (md.getOrderType() == Constants.MDD_ORDER_GROUP_BUY_TYPE) {
                    marketMddOrdersRpMddOrder.setOrderTitle(md.getGroupName());
                    marketMddOrdersRpMddOrder.setVerWay(md.getVerWay());
                    marketMddOrdersRpMddOrder.setVerTime(md.getVerTime());
                } else if (md.getOrderType() == Constants.MDD_ORDER_MAN_DAN_TYPE) {
                    marketMddOrdersRpMddOrder.setOrderTitle(String.format("优惠力度：%s折", md.getPayContent()));
                    marketMddOrdersRpMddOrder.setCouponType(md.getCouponType());
                    marketMddOrdersRpMddOrder.setPayTime(md.getPayTime());
                    if(md.getCouponType() == Constants.MDD_ONLINE_TYPE){
                        marketMddOrdersRpMddOrder.setOrderTitle("优惠力度：无优惠");
                    }
                }
                marketMddOrdersRpMddOrder.setActualConsum(md.getActualConsum());
                marketMddOrdersRpVo.getOrders().add(marketMddOrdersRpMddOrder);
            }
        }
        Integer count = getByBusinessOrderForDataBankDto.getCount();
        marketMddOrdersRpVo.setCount(count.longValue());
        if (count > unifiedCodeQueryVo.getPageNum() * unifiedCodeQueryVo.getPageSize()) {
            marketMddOrdersRpVo.setIsLastPage(Constants.COMMON_FLAG_FALSE_INT);
        }
        return ResultMapHelper.success(marketMddOrdersRpVo, ResultMsg.MSG_GET_SUCCESS);
    }


    @Override
    public Map<String, Object> minAppOrders(MinAppOrdersRqVo minAppOrdersRqVo) {
        MinAppOrdersRpVo minAppOrdersRpVo = new MinAppOrdersRpVo();
        Map<String, Object> userDate = new HashMap<>();
        DxshAppUserChannel dxshAppUserChannel = dxshAppUserChannelService.getById(minAppOrdersRqVo.getChannelId());
        if (dxshAppUserChannel == null) {
            BeanUtils.copyProperties(minAppOrdersRpVo, minAppOrdersRpVo);
            minAppOrdersRpVo.setCount(0L);
            minAppOrdersRpVo.setOrders(new ArrayList<>());
            userDate.put("minAppType", null);
        } else {
            if(dxshAppUserChannel.getStoreId() == null){
                BeanUtils.copyProperties(minAppOrdersRpVo, minAppOrdersRpVo);
                minAppOrdersRpVo.setCount(0L);
                minAppOrdersRpVo.setOrders(new ArrayList<>());
                userDate.put("minAppType", null);
            }else{
                Map<String, Object> orderByPage = minAppOrderService.getOrderByPage(Integer.parseInt(dxshAppUserChannel.getStoreId()), minAppOrdersRqVo.getStatus(),
                        minAppOrdersRqVo.getPageNo(), minAppOrdersRqVo.getPageSize());

                log.info("orderByPage：" + orderByPage);

                if (orderByPage.get("status") != null && orderByPage.get("status").equals("00")) {
                    Map<String, Object> dataMap = (Map) orderByPage.get("data");
                    Map<String, Object> orderMap = (Map) dataMap.get("orderList");
                    List<MinAppOrdersRpVo.MinAppOrdersData> records = JSON.parseArray(JSON.toJSONString(orderMap.get("records")), MinAppOrdersRpVo.MinAppOrdersData.class);
                    for(MinAppOrdersRpVo.MinAppOrdersData m : records){
                        if(m.getPaymentTime() != null){
                            String paymentTime = m.getPaymentTime();
                            Date paymentDate = DateTimeUtils.converStringToDate(paymentTime,DateTimeUtils.DATE_PATTERN_DAY_14);
                            LocalDateTime paymentTimeStamp = DateTimeUtils.converDateToLocalDateTime(paymentDate);
                            m.setPaymentTimeFont(paymentTimeStamp);
                        }
                        if(m.getCreateTime() != null){
                            m.setCreateTimeFont(DateTimeUtils.converDateToLocalDateTime(DateTimeUtils.converStringToDate(m.getCreateTime(),DateTimeUtils.DATE_PATTERN_DAY_14)));
                        }
                        if(m.getExpectedTime() != null){
                            m.setExpectedTimeFont(DateTimeUtils.converDateToLocalDateTime(DateTimeUtils.converStringToDate(m.getExpectedTime(),DateTimeUtils.DATE_PATTERN_DAY_14)));
                        }
                    }
                    minAppOrdersRpVo.setOrders(records);

                    Object orderCount = ((Map) dataMap.get("orderCount")).get(orderTypeMap.get(minAppOrdersRqVo.getStatus()));
                    minAppOrdersRpVo.setCount(orderCount == null ? 0 : Long.parseLong(String.valueOf(orderCount)));
                }
                userDate.put("minAppType", dxshAppUserChannel.getDemoId());
            }
        }
        return ResultMapHelper.success(minAppOrdersRpVo, userDate, ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    public Map<String, Object> dataBankAnalysisRevenue(UnifiedCodeQueryVo unifiedCodeQueryVo) {
        Date nowTime = new Date();
        DataBankAnalysisRevenueRpVo dataBankAnalysisRevenueRpVo = new DataBankAnalysisRevenueRpVo();
        //装载测试数据
        if(userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()).equals(CDPUtils.superAccountId)) {
            dataBankAnalysisRevenueRpVo = CDPSimDataUtils.getCdpAllBo().getRevenue();
            return ResultMapHelper.success(dataBankAnalysisRevenueRpVo, ResultMsg.MSG_GET_SUCCESS);
        }
        //        dataBankAnalysisRevenueRpVo.getRevenueData().setCumulativeFlow(1000d);
//        dataBankAnalysisRevenueRpVo.getRevenueData().setTodayRunningMoney(40d);
//        dataBankAnalysisRevenueRpVo.getRevenueData().setWeekRunningMoney(4896d);
//        dataBankAnalysisRevenueRpVo.getHistoricalRevenueData().setYesRunningAllMoney(500d);
//        dataBankAnalysisRevenueRpVo.getHistoricalRevenueData().setYesterdayLineData(init(1, DateTimeUtils.getNowTimeStamp()));

        CdpUtilsBo cdpUtilsBo = new CdpUtilsBo();

        String searchDate = DateTimeUtils.converDateToString(nowTime, DateTimeUtils.DATE_PATTERN_DAY);
        //TODO 历史流水
        CDPUtils.orderOrderTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), searchDate, searchDate);
        dataBankAnalysisRevenueRpVo.getRevenueData().setTodayRunningMoney(cdpUtilsBo.getTotalAmount().doubleValue());
        dataBankAnalysisRevenueRpVo.getRevenueData().setWeekRunningMoney(dankCdpOrderTotalService.countWeekTotalAmount(unifiedCodeQueryVo.getUserId()));
        dataBankAnalysisRevenueRpVo.getRevenueData().setCumulativeFlow(dankCdpOrderTotalService.countAllTotalAmount(unifiedCodeQueryVo.getUserId()).doubleValue());

        //曲线图操作（昨日曲线）
        String endDate = DateTimeUtils.converDateToString(nowTime,DateTimeUtils.DATE_PATTERN_DAY);
        Integer endTimestamp = DateTimeUtils.converDateToInterger(DateTimeUtils.getStartTimeOfDay(nowTime));
        Integer startTimestamp = DateTimeUtils.converDateToInterger(DateTimeUtils.addDay(DateTimeUtils.converIntegerToDate(endTimestamp), -1));
        String startDate = DateTimeUtils.converDateToString(DateTimeUtils.addDay(nowTime,-1),DateTimeUtils.DATE_PATTERN_DAY);
        CDPUtils.orderOrderTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startDate, startDate);
        dataBankAnalysisRevenueRpVo.getHistoricalRevenueData().setYesRunningAllMoney(cdpUtilsBo.getTotalAmount().doubleValue());
        dankCdpOrderTotalService.getYesDayData(cdpUtilsBo,unifiedCodeQueryVo.getUserId(),"orderAmount");
        dataBankAnalysisRevenueRpVo.getHistoricalRevenueData().setYesterdayLineData(initLineDataDesc(STAT_HOUR, startTimestamp, cdpUtilsBo.getYesLineData()));

        //曲线图操作（7日曲线）
        startDate = DateTimeUtils.converDateToString(DateTimeUtils.addDay(nowTime,-7),DateTimeUtils.DATE_PATTERN_DAY);
        dankCdpOrderTotalService.getOrderTotalTotalAmountByGroup(cdpUtilsBo,unifiedCodeQueryVo.getUserId(), startDate, endDate,"day");
        dataBankAnalysisRevenueRpVo.getHistoricalRevenueData().setSevenLineData(initLineDataDesc(STAT_WEEK, endTimestamp, cdpUtilsBo.getGroupLineData()));
        dataBankAnalysisRevenueRpVo.getHistoricalRevenueData().setSevenRunningAllMoney(cdpUtilsBo.getTotalAmount().doubleValue());

        //曲线图操作（30日曲线）
        startDate = DateTimeUtils.converDateToString(DateTimeUtils.addDay(nowTime,-30),DateTimeUtils.DATE_PATTERN_DAY);
        dankCdpOrderTotalService.getOrderTotalTotalAmountByGroup(cdpUtilsBo,unifiedCodeQueryVo.getUserId(), startDate, endDate,"day");
        dataBankAnalysisRevenueRpVo.getHistoricalRevenueData().setThirtyLineData(initLineDataDesc(STAT_THIRD, endTimestamp, cdpUtilsBo.getGroupLineData()));
        dataBankAnalysisRevenueRpVo.getHistoricalRevenueData().setThirtyRunningAllMoney(cdpUtilsBo.getTotalAmount().doubleValue());
        //半年
        startDate = DateTimeUtils.converDateToString(DateTimeUtils.addMonth(nowTime,-6),DateTimeUtils.DATE_PATTERN_DAY);
        dankCdpOrderTotalService.getOrderTotalTotalAmountByGroup(cdpUtilsBo,unifiedCodeQueryVo.getUserId(), startDate, endDate,"month");
        dataBankAnalysisRevenueRpVo.getHistoricalRevenueData().setHalfYearLineData(initLineDataDesc(STAT_MONTH, endTimestamp, cdpUtilsBo.getGroupLineData()));
        dataBankAnalysisRevenueRpVo.getHistoricalRevenueData().setHalfYearRunningAllMoney(cdpUtilsBo.getTotalAmount().doubleValue());

//        dataBankAnalysisRevenueRpVo.getHistoricalRevenueData().setYesterdayLineData(init(1, DateTimeUtils.getNowTimeStamp()));
//        dataBankAnalysisRevenueRpVo.getHistoricalRevenueData().setSevenLineData(init(2, DateTimeUtils.getNowTimeStamp()));
//        dataBankAnalysisRevenueRpVo.getHistoricalRevenueData().setThirtyLineData(init(3, DateTimeUtils.getNowTimeStamp()));
//        dataBankAnalysisRevenueRpVo.getHistoricalRevenueData().setHalfYearLineData(init(4, DateTimeUtils.getNowTimeStamp()));
        return ResultMapHelper.success(dataBankAnalysisRevenueRpVo, ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    public Map<String, Object> dataBankAnalysisOrder(UnifiedCodeQueryVo unifiedCodeQueryVo) {
        DataBankAnalysisOrderRpVo dataBankAnalysisOrderRpVo = new DataBankAnalysisOrderRpVo();
        if(userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()).equals(CDPUtils.superAccountId)) {
            dataBankAnalysisOrderRpVo = CDPSimDataUtils.getCdpAllBo().getOrder();
            return ResultMapHelper.success(dataBankAnalysisOrderRpVo, ResultMsg.MSG_GET_SUCCESS);
        }
        dataBankAnalysisOrderExample(unifiedCodeQueryVo, dataBankAnalysisOrderRpVo);

//        Integer time = DateTimeUtils.converDateToInterger(DateTimeUtils.addDay(new Date(),1));
//        dataBankAnalysisOrderRpVo.getCusOrder().setYesOrderLineData(initTwoLineData(1,time,null,null));
//        dataBankAnalysisOrderRpVo.getCusOrder().setSevenOrderLineData(initTwoLineData(2,time,null,null));
//        dataBankAnalysisOrderRpVo.getCusOrder().setThirtyOrderLineData(initTwoLineData(3,time,null,null));
//        dataBankAnalysisOrderRpVo.getCusOrder().setHalfYearOrderLineData(initTwoLineData(4,time,null,null));

        //装载测试数据
//        if(userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()).equals(CDPUtils.superAccountId)) {
//            dataBankAnalysisOrderRpVo = CDPSimDataUtils.cdpAllBo.getOrder();
//
//            //dataBankAnalysisOrderRpVo.getCusOrder().setYesOrderLineData(init);
////            dataBankAnalysisRevenueRpVo.getHistoricalRevenueData().setYesterdayLineData(init(1, DateTimeUtils.getNowTimeStamp()));
////            dataBankAnalysisRevenueRpVo.getHistoricalRevenueData().setSevenLineData(init(2, DateTimeUtils.getNowTimeStamp()));
////            dataBankAnalysisRevenueRpVo.getHistoricalRevenueData().setThirtyLineData(init(3, DateTimeUtils.getNowTimeStamp()));
////            dataBankAnalysisRevenueRpVo.getHistoricalRevenueData().setHalfYearLineData(init(4, DateTimeUtils.getNowTimeStamp()));
//        }
        return ResultMapHelper.success(dataBankAnalysisOrderRpVo, ResultMsg.MSG_GET_SUCCESS);
    }

    private void dataBankAnalysisOrderExample(UnifiedCodeQueryVo unifiedCodeQueryVo, DataBankAnalysisOrderRpVo dataBankAnalysisOrderRpVo) {
//        dataBankAnalysisOrderRpVo.setTodayUnitPrice(5000d);
//        dataBankAnalysisOrderRpVo.setOrdersTodayNum(100);
//        //订单与客单
//        dataBankAnalysisOrderRpVo.getCusOrder().setYesOrderNum(100);
//        dataBankAnalysisOrderRpVo.getCusOrder().setYesCusOrderPrice(100d);
//        dataBankAnalysisOrderRpVo.getCusOrder().setSevenOrderNum(100);
//        dataBankAnalysisOrderRpVo.getCusOrder().setSevenCusOrderPrice(200d);
//        dataBankAnalysisOrderRpVo.getCusOrder().setThirtyOrderNum(90);
//        dataBankAnalysisOrderRpVo.getCusOrder().setThirtyCusOrderPrice(1000d);
//        dataBankAnalysisOrderRpVo.getCusOrder().setHalfYearOrderNum(8009);
//        dataBankAnalysisOrderRpVo.getCusOrder().setHalfYearCusOrderPrice(8987949D);
//        dataBankAnalysisOrderRpVo.getCusOrder().getYesOrderLineData().addAll(initTwoLine(1, DateTimeUtils.getNowTimeStamp()));
//        dataBankAnalysisOrderRpVo.getCusOrder().getSevenOrderLineData().addAll(initTwoLine(2, DateTimeUtils.getNowTimeStamp()));
//        dataBankAnalysisOrderRpVo.getCusOrder().getThirtyOrderLineData().addAll(initTwoLine(3, DateTimeUtils.getNowTimeStamp()));
//        dataBankAnalysisOrderRpVo.getCusOrder().getHalfYearOrderLineData().addAll(initTwoLine(4, DateTimeUtils.getNowTimeStamp()));
//        dataBankAnalysisOrderRpVo.getDeliveryMethod().getSevenDeliveryMethodData().addAll(initPie(2, DateTimeUtils.getNowTimeStamp(), new String[]{"第三方配送",
//                "自提", "快递"}));
//        dataBankAnalysisOrderRpVo.getTurnoverRate().getSevenTurnoverRateData().addAll(init(2, DateTimeUtils.getNowTimeStamp(), 100));
//        dataBankAnalysisOrderRpVo.getTurnoverRate().getThirtyTurnoverRateData().addAll(init(3, DateTimeUtils.getNowTimeStamp(), 100));
//        dataBankAnalysisOrderRpVo.getTurnoverRate().getHalfYearTurnoverRateData().addAll(init(4, DateTimeUtils.getNowTimeStamp(), 100));

        Date nowTime = new Date();
        CdpUtilsBo cdpUtilsBo = new CdpUtilsBo();
        String endDate = DateTimeUtils.converDateToString(nowTime, DateTimeUtils.DATE_PATTERN_DAY);
        //今日订单
        CDPUtils.orderOrderTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), endDate, endDate);
        dataBankAnalysisOrderRpVo.setOrdersTodayNum(cdpUtilsBo.getOrderQuantity().intValue());
        //今日客单
        CDPUtils.orderPerCustomerTransaction(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), endDate, endDate);
        dataBankAnalysisOrderRpVo.setTodayUnitPrice(cdpUtilsBo.getOrderQuantity().doubleValue());
        //昨日订单客单
        String startDate = DateTimeUtils.converDateToString(DateTimeUtils.addDay(nowTime, -1), DateTimeUtils.DATE_PATTERN_DAY);
        CDPUtils.orderOrderTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startDate, startDate);
        dataBankAnalysisOrderRpVo.getCusOrder().setYesOrderNum(cdpUtilsBo.getOrderQuantity().intValue());
        CDPUtils.orderPerCustomerTransaction(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startDate, startDate);
        dataBankAnalysisOrderRpVo.getCusOrder().setYesCusOrderPrice(ArithUtil.round(cdpUtilsBo.getOrderQuantity().doubleValue(), 2));
        //昨日订单（曲线图）
        dankCdpOrderTotalService.getYesDayData(cdpUtilsBo,unifiedCodeQueryVo.getUserId(),"orderQuantity");
        Map<String,Double> orderYesLineData = new HashMap<>();
        orderYesLineData.putAll(cdpUtilsBo.getYesLineData());
        //昨日客单（曲线图）
        dankCdpOrderTotalService.getYesDayData(cdpUtilsBo,unifiedCodeQueryVo.getUserId(),"unitPrice");
        Map<String,Double> cusOrderYesLineData = new HashMap<>();
        cusOrderYesLineData.putAll(cdpUtilsBo.getYesLineData());
        //昨日订单配送方式
        CDPUtils.orderDeliveryTypeTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startDate, endDate);
        dataBankAnalysisOrderRpVo.getDeliveryMethod().setYesDeliveryMethodData(initorderDeliveryTypeTotalPie(cdpUtilsBo.getDeliveryTypeTotalBos()));

        //7天内订单
        startDate = DateTimeUtils.converDateToString(DateTimeUtils.addDay(nowTime, -7), DateTimeUtils.DATE_PATTERN_DAY);
        CDPUtils.orderOrderTotalGroup(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startDate, endDate,"day");
        dataBankAnalysisOrderRpVo.getCusOrder().setSevenOrderNum(cdpUtilsBo.getOrderQuantity().intValue());
        dataBankAnalysisOrderRpVo.getCusOrder().setSevenOrderLineData(initTwoLineData(STAT_WEEK,DateTimeUtils.converDateToInterger(nowTime),cdpUtilsBo.getGroupLineData(),new HashMap<>()));
        //7天内客单
        CDPUtils.orderPerCustomerTransactionGroup(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startDate, endDate,"day");
        dataBankAnalysisOrderRpVo.getCusOrder().setSevenCusOrderPrice(ArithUtil.round(cdpUtilsBo.getOrderQuantity().doubleValue(), 2));
        dataBankAnalysisOrderRpVo.getCusOrder().setSevenOrderLineData(initTwoLineData(STAT_WEEK,DateTimeUtils.converDateToInterger(nowTime),new HashMap<>(),cdpUtilsBo.getGroupLineData()));
        //7天内订单配送方式
        CDPUtils.orderDeliveryTypeTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startDate, endDate);
        dataBankAnalysisOrderRpVo.getDeliveryMethod().setSevenDeliveryMethodData(initorderDeliveryTypeTotalPie(cdpUtilsBo.getDeliveryTypeTotalBos()));

//        OrderDeliveryTypeTotalBo orderDeliveryTypeTotalBo1 = new OrderDeliveryTypeTotalBo();
//        orderDeliveryTypeTotalBo1.setDeliveryType("第三方配送");
//        orderDeliveryTypeTotalBo1.setOrderNumber(10);
//        cdpUtilsBo.getDeliveryTypeTotalBos().add(orderDeliveryTypeTotalBo1);
//        OrderDeliveryTypeTotalBo orderDeliveryTypeTotalBo2 = new OrderDeliveryTypeTotalBo();
//        orderDeliveryTypeTotalBo2.setDeliveryType("自提");
//        orderDeliveryTypeTotalBo2.setOrderNumber(60);
//        cdpUtilsBo.getDeliveryTypeTotalBos().add(orderDeliveryTypeTotalBo2);
//
//        OrderDeliveryTypeTotalBo orderDeliveryTypeTotalBo3 = new OrderDeliveryTypeTotalBo();
//        orderDeliveryTypeTotalBo3.setDeliveryType("快递");
//        orderDeliveryTypeTotalBo3.setOrderNumber(90);
//        cdpUtilsBo.getDeliveryTypeTotalBos().add(orderDeliveryTypeTotalBo3);


        //7天内的用餐人数
        CDPUtils.haveDinnerUserTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startDate, endDate);
        dataBankAnalysisOrderRpVo.getDiningPopulation().setSevenPieData(initHaveDinnerUserTotalPie(cdpUtilsBo.getDeliveryTypeTotalBos()));
        //7天内的配送距离
        CDPUtils.OrderDeliveryDistanceTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startDate, endDate);
        dataBankAnalysisOrderRpVo.getDistributionDistance().setSevenDistributionDistanceData(orderDeliveryDistanceTotal(cdpUtilsBo.getDeliveryTypeTotalBos()));


        //30天
        startDate = DateTimeUtils.converDateToString(DateTimeUtils.addDay(nowTime, -30), DateTimeUtils.DATE_PATTERN_DAY);
        CDPUtils.orderOrderTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startDate, endDate);
        dataBankAnalysisOrderRpVo.getCusOrder().setThirtyOrderNum(cdpUtilsBo.getOrderQuantity().intValue());
        CDPUtils.orderPerCustomerTransaction(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startDate, endDate);
        dataBankAnalysisOrderRpVo.getCusOrder().setThirtyCusOrderPrice(ArithUtil.round(cdpUtilsBo.getOrderQuantity().doubleValue(), 2));
        //30天配送方式
        CDPUtils.orderDeliveryTypeTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startDate, endDate);
        dataBankAnalysisOrderRpVo.getDeliveryMethod().setThirtyDeliveryMethodData(initorderDeliveryTypeTotalPie(cdpUtilsBo.getDeliveryTypeTotalBos()));
        //30天用餐人数
        CDPUtils.haveDinnerUserTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startDate, endDate);
        dataBankAnalysisOrderRpVo.getDiningPopulation().setThirtyPieData(initHaveDinnerUserTotalPie(cdpUtilsBo.getDeliveryTypeTotalBos()));
        //30天内的配送距离
        CDPUtils.OrderDeliveryDistanceTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startDate, endDate);
        dataBankAnalysisOrderRpVo.getDistributionDistance().setThirtyDistributionDistanceData(orderDeliveryDistanceTotal(cdpUtilsBo.getDeliveryTypeTotalBos()));


        //半年内
        startDate = DateTimeUtils.converDateToString(DateTimeUtils.addMonth(nowTime, -6), DateTimeUtils.DATE_PATTERN_DAY);
        CDPUtils.orderOrderTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startDate, endDate);
        dataBankAnalysisOrderRpVo.getCusOrder().setHalfYearOrderNum(cdpUtilsBo.getOrderQuantity().intValue());
        CDPUtils.orderPerCustomerTransaction(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startDate, endDate);
        dataBankAnalysisOrderRpVo.getCusOrder().setHalfYearCusOrderPrice(ArithUtil.round(cdpUtilsBo.getOrderQuantity().doubleValue(), 2));

        //半年配送方式
        CDPUtils.orderDeliveryTypeTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startDate, endDate);
        dataBankAnalysisOrderRpVo.getDeliveryMethod().setThirtyDeliveryMethodData(initorderDeliveryTypeTotalPie(cdpUtilsBo.getDeliveryTypeTotalBos()));
        //半年内用餐人数
        CDPUtils.haveDinnerUserTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startDate, endDate);
        dataBankAnalysisOrderRpVo.getDiningPopulation().setHalfYearPieData(initHaveDinnerUserTotalPie(cdpUtilsBo.getDeliveryTypeTotalBos()));
        //半年内的配送距离
        CDPUtils.OrderDeliveryDistanceTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startDate, endDate);
        dataBankAnalysisOrderRpVo.getDistributionDistance().setHalfYearDistributionDistanceData(orderDeliveryDistanceTotal(cdpUtilsBo.getDeliveryTypeTotalBos()));

    }

    public List<LineData> init(Integer statType, Integer timestamp) {
        LinkedList<LineData> lineData = new LinkedList<>();
        String[] weeks = new String[]{"", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
        if (statType.equals(1)) {
            for (int hour = 0; hour <= 24; hour++) {
                LineData lk = new LineData();
                lk.setTitle(String.format("0%s:00", hour));
                lk.setSmallTitle(String.format("%s 0%s:00-0%s:59",
                        DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp),
                                DateTimeUtils.DATE_PATTERN_DAY_10), hour, hour));
                if (hour >= 10) {
                    lk.setSmallTitle(String.format("%s %s:00-%s:59",
                            DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp),
                                    DateTimeUtils.DATE_PATTERN_DAY_10), hour, hour));
                    lk.setTitle(String.format("%s:00", hour));
                }
                lk.setValue(ArithUtil.round(Math.random() * 100, 0));
                lineData.add(lk);
            }
        } else if (statType.equals(2)) {
            Integer dayNum = DateTimeUtils.getThisMonthDayNum(timestamp);
            Date nowTime = DateTimeUtils.converIntegerToDate(timestamp);

            for (int ii = 1; ii <= 7; ii++) {
                LineData lk = new LineData();
                lk.setTitle(DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_DAY_10));
                lk.setSmallTitle(String.format("%s %s",
                        DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp),
                                DateTimeUtils.DATE_PATTERN_DAY), weeks[DateTimeUtils.getDayOfWeek(DateTimeUtils.converIntegerToDate(timestamp))]));
                lk.setValue(ArithUtil.round(Math.random() * 100, 0));
                lineData.addFirst(lk);
                timestamp = timestamp - 60 * 60 * 24;
            }
        } else if (statType.equals(3)) {
            Integer dayNum = DateTimeUtils.getThisMonthDayNum(timestamp);
            Date nowTime = DateTimeUtils.converIntegerToDate(timestamp);

            for (int ii = 1; ii <= dayNum; ii++) {
                LineData lk = new LineData();
                lk.setTitle(DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_DAY_10));
                lk.setSmallTitle(String.format("%s %s",
                        DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp),
                                DateTimeUtils.DATE_PATTERN_DAY), weeks[DateTimeUtils.getDayOfWeek(DateTimeUtils.converIntegerToDate(timestamp))]));
                lk.setValue(ArithUtil.round(Math.random() * 100, 0));
                lineData.addFirst(lk);
                timestamp = timestamp - 60 * 60 * 24;
            }
        } else if (statType.equals(4)) {
            for (int ii = 1; ii <= 6; ii++) {
                LineData lk = new LineData();
                lk.setTitle(DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_MONTH_01));
                lk.setSmallTitle(DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_MONTH_01));
                lk.setValue(ArithUtil.round(Math.random() * 100, 0));
                lineData.addFirst(lk);
                timestamp = DateTimeUtils.converDateToInterger(DateTimeUtils.addMonth(DateTimeUtils.converIntegerToDate(timestamp), -1));
            }
        }
        return lineData;
    }

    public List<LineData> initLineDataDesc(Integer statType, Integer timestamp, Map<String, Double> dbLineData) {
        List<LineData> lineData = new ArrayList<>();
        String[] weeks = new String[]{"", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
        if (statType.equals(STAT_HOUR)) {
            for (int hour = 0; hour < 24; hour++) {
                LineData lk = new LineData();
                String hourDesc = String.format("0%s", hour);
                lk.setTitle(String.format("%s:00", hourDesc));
                lk.setSmallTitle(String.format("%s 0%s:00-0%s:59",
                        DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp),
                                DateTimeUtils.DATE_PATTERN_DAY_10), hour, hour));
                if (hour >= 10) {
                    lk.setSmallTitle(String.format("%s %s:00-%s:59",
                            DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp),
                                    DateTimeUtils.DATE_PATTERN_DAY_10), hour, hour));
                    hourDesc = String.format("%s", hour);
                    lk.setTitle(String.format("%s:00", hourDesc));
                }
                if (dbLineData.containsKey(hourDesc)) {
                    lk.setValue(dbLineData.get(hourDesc));
                }
                lineData.add(lk);
            }
        } else if (statType.equals(STAT_WEEK)) {
            Date time = DateTimeUtils.converIntegerToDate(timestamp);
            for (int ii = 1; ii <= 7; ii++) {
                LineData lk = new LineData();
                lk.setTitle(DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_DAY_10));
                lk.setSmallTitle(String.format("%s %s",
                        DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp),
                                DateTimeUtils.DATE_PATTERN_DAY), weeks[DateTimeUtils.getDayOfWeek(DateTimeUtils.converIntegerToDate(timestamp))]));
                String weekDesc = DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_DAY_12);
                if (dbLineData.containsKey(weekDesc)) {
                    lk.setValue(dbLineData.get(weekDesc));
                }
                lineData.add(lk);
                timestamp = timestamp - 60 * 60 * 24;
            }
        } else if (statType.equals(STAT_THIRD)) {
            for (int ii = 1; ii <= 30; ii++) {
                LineData lk = new LineData();
                lk.setTitle(DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_DAY_10));
                lk.setSmallTitle(lk.getTitle());

                String dayDesc = DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_DAY);
                if (dbLineData.containsKey(dayDesc)) {
                    lk.setValue(dbLineData.get(dayDesc));
                }
                lineData.add(lk);
                timestamp = timestamp - 60 * 60 * 24;
            }
        } else if (statType.equals(STAT_MONTH)) {
            for (int ii = 1; ii <= 6; ii++) {
                LineData lk = new LineData();
                lk.setTitle(DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_MONTH_01));
                lk.setSmallTitle(DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_MONTH_01));
                String monthDesc = DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_MONTH_1);
                if (dbLineData.containsKey(monthDesc)) {
                    lk.setValue(dbLineData.get(monthDesc));
                }
                lineData.add(lk);
                timestamp = DateTimeUtils.converDateToInterger(DateTimeUtils.addMonth(DateTimeUtils.converIntegerToDate(timestamp), -1));
            }
        }
        return lineData;
    }

    public List<LineData> init(Integer statType, Integer timestamp, Integer range) {
        List<LineData> lineData = new ArrayList<>();
        String[] weeks = new String[]{"", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
        if (statType.equals(1)) {
            for (int hour = 0; hour <= 24; hour++) {
                LineData lk = new LineData();
                lk.setTitle(String.format("0%s:00", hour));
                lk.setSmallTitle(String.format("%s 0%s:00-0%s:59",
                        DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp),
                                DateTimeUtils.DATE_PATTERN_DAY_10), hour, hour));
                if (hour >= 10) {
                    lk.setSmallTitle(String.format("%s %s:00-%s:59",
                            DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp),
                                    DateTimeUtils.DATE_PATTERN_DAY_10), hour, hour));
                    lk.setTitle(String.format("%s:00", hour));
                }
                lk.setValue(ArithUtil.round(Math.random() * range, 0));
                lineData.add(lk);
            }
        } else if (statType.equals(2)) {
            Integer dayNum = DateTimeUtils.getThisMonthDayNum(timestamp);
            Date nowTime = DateTimeUtils.converIntegerToDate(timestamp);

            for (int ii = 1; ii <= 7; ii++) {
                LineData lk = new LineData();
                lk.setTitle(DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_DAY_10));
                lk.setSmallTitle(String.format("%s %s",
                        DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp),
                                DateTimeUtils.DATE_PATTERN_DAY), weeks[DateTimeUtils.getDayOfWeek(DateTimeUtils.converIntegerToDate(timestamp))]));
                lk.setValue(ArithUtil.round(Math.random() * range, 0));
                lineData.add(lk);
                timestamp = timestamp - 60 * 60 * 24;
            }
        } else if (statType.equals(3)) {
            Integer dayNum = DateTimeUtils.getThisMonthDayNum(timestamp);
            Date nowTime = DateTimeUtils.converIntegerToDate(timestamp);

            for (int ii = 1; ii <= dayNum; ii++) {
                LineData lk = new LineData();
                lk.setTitle(DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_DAY_10));
                lk.setSmallTitle(String.format("%s %s",
                        DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp),
                                DateTimeUtils.DATE_PATTERN_DAY), weeks[DateTimeUtils.getDayOfWeek(DateTimeUtils.converIntegerToDate(timestamp))]));
                lk.setValue(ArithUtil.round(Math.random() * range, 0));
                lineData.add(lk);
                timestamp = timestamp - 60 * 60 * 24;
            }
        } else if (statType.equals(4)) {
            for (int ii = 1; ii <= 6; ii++) {
                LineData lk = new LineData();
                lk.setTitle(DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_MONTH_01));
                lk.setSmallTitle(DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_MONTH_01));
                lk.setValue(ArithUtil.round(Math.random() * range, 0));
                lineData.add(lk);
                timestamp = DateTimeUtils.converDateToInterger(DateTimeUtils.addMonth(DateTimeUtils.converIntegerToDate(timestamp), -1));
            }
        }
        return lineData;
    }

    public List<DataBankAnalysisOrderRpVo.CusOrderLineData> initTwoLineData(Integer statType, Integer timestamp,Map<String,Double> valuesFirstMap,
                                                                        Map<String,Double> valueSecondMap) {
        LinkedList<DataBankAnalysisOrderRpVo.CusOrderLineData> lineData = new LinkedList<>();
        String[] weeks = new String[]{"", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
        if (statType.equals(1)) {
            for (int hour = 0; hour < 24; hour++) {
                DataBankAnalysisOrderRpVo.CusOrderLineData lk = new DataBankAnalysisOrderRpVo.CusOrderLineData();
                lk.setTitle(String.format("0%s:00", hour));
                lk.setSmallTitle(String.format("%s 0%s:00-0%s:59",
                        DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp),
                                DateTimeUtils.DATE_PATTERN_DAY_10), hour, hour));
                if (hour >= 10) {
                    lk.setSmallTitle(String.format("%s %s:00-%s:59",
                            DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp),
                                    DateTimeUtils.DATE_PATTERN_DAY_10), hour, hour));
                    lk.setTitle(String.format("%s:00", hour));
                }

                lk.setValue1(ArithUtil.round(Math.random() * 100, 0));
                lk.setValue2((int) (Math.random() * 100));
                lineData.add(lk);
            }
        } else if (statType.equals(STAT_WEEK)) {
            for (int ii = 1; ii <= 7; ii++) {
                DataBankAnalysisOrderRpVo.CusOrderLineData lk = new DataBankAnalysisOrderRpVo.CusOrderLineData();
                lk.setTitle(DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_DAY_10));
                lk.setSmallTitle(String.format("%s %s",
                        DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp),
                                DateTimeUtils.DATE_PATTERN_DAY), weeks[DateTimeUtils.getDayOfWeek(DateTimeUtils.converIntegerToDate(timestamp))]));
                String dayDesc = DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_DAY);
//                if (valuesFirstMap.containsKey(dayDesc)) {
//                    lk.setValue1(valuesFirstMap.get(dayDesc));
//                }
//                if (valueSecondMap.containsKey(dayDesc)) {
//                    lk.setValue2(valueSecondMap.get(dayDesc).intValue());
//                }
                lk.setValue1(ArithUtil.round(Math.random() * 100, 0));
                lk.setValue2((int) (Math.random() * 100));
                lineData.addFirst(lk);
                timestamp = timestamp - 60 * 60 * 24;
            }
        } else if (statType.equals(3)) {
            Integer dayNum = DateTimeUtils.getThisMonthDayNum(timestamp);
            Date nowTime = DateTimeUtils.converIntegerToDate(timestamp);

            for (int ii = 1; ii <= dayNum; ii++) {
                DataBankAnalysisOrderRpVo.CusOrderLineData lk = new DataBankAnalysisOrderRpVo.CusOrderLineData();
                lk.setTitle(DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_DAY_10));
                lk.setSmallTitle(String.format("%s %s",
                        DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp),
                                DateTimeUtils.DATE_PATTERN_DAY), weeks[DateTimeUtils.getDayOfWeek(DateTimeUtils.converIntegerToDate(timestamp))]));
                lk.setValue1(ArithUtil.round(Math.random() * 100, 0));
                lk.setValue2((int) (Math.random() * 100));
                timestamp = timestamp - 60 * 60 * 24;
                lineData.addFirst(lk);
            }
        } else if (statType.equals(4)) {
            for (int ii = 1; ii <= 6; ii++) {
                DataBankAnalysisOrderRpVo.CusOrderLineData lk = new DataBankAnalysisOrderRpVo.CusOrderLineData();
                lk.setTitle(DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_MONTH_01));
                lk.setSmallTitle(DateTimeUtils.converDateToString(DateTimeUtils.converIntegerToDate(timestamp), DateTimeUtils.DATE_PATTERN_MONTH_01));
                lk.setValue1(ArithUtil.round(Math.random() * 100, 0));
                lk.setValue2((int) (Math.random() * 100));
                lineData.addFirst(lk);
                timestamp = DateTimeUtils.converDateToInterger(DateTimeUtils.addMonth(DateTimeUtils.converIntegerToDate(timestamp), -1));
            }
        }
        return lineData;
    }

    public List<PieData> initPie(List<OrderDeliveryTypeTotalBo> deliveryTypeTotalBos) {

        List<PieData> lineData = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(deliveryTypeTotalBos)){
            Integer count = deliveryTypeTotalBos.stream().collect(Collectors.summingInt(OrderDeliveryTypeTotalBo::getOrderNumber));
            for(OrderDeliveryTypeTotalBo orderDeliveryTypeTotalBo : deliveryTypeTotalBos){
                PieData pieData = new PieData();
                pieData.setTitle(orderDeliveryTypeTotalBo.getDeliveryType());
                pieData.setValue(orderDeliveryTypeTotalBo.getOrderNumber().doubleValue());
                pieData.setRate(ArithUtil.div(pieData.getValue(),count,2));
                lineData.add(pieData);

            }
        }
        return lineData;
    }


    public List<PieData> orderDeliveryDistanceTotal(List<OrderDeliveryTypeTotalBo> deliveryTypeTotalBos){
        List<PieData> lineData = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(deliveryTypeTotalBos)){
            Integer count = deliveryTypeTotalBos.stream().collect(Collectors.summingInt(OrderDeliveryTypeTotalBo::getOrderNumber));
            for(OrderDeliveryTypeTotalBo orderDeliveryTypeTotalBo : deliveryTypeTotalBos){
                PieData pieData = new PieData();
                pieData.setTitle(orderDeliveryTypeTotalBo.getDeliveryType());
                pieData.setValue(orderDeliveryTypeTotalBo.getOrderNumber().doubleValue());
                pieData.setRate(ArithUtil.div(pieData.getValue(),count,2));
                pieData.setSmallTitle(String.format("%s",pieData.getValue().intValue(),new BigDecimal(ArithUtil.mul(pieData.getRate(),100)).intValue() + "%"));
                lineData.add(pieData);
            }
        }
        return lineData;
    }

    public List<PieData> initorderDeliveryTypeTotalPie(List<OrderDeliveryTypeTotalBo> deliveryTypeTotalBos){
        List<PieData> lineData = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(deliveryTypeTotalBos)){
            Integer count = deliveryTypeTotalBos.stream().collect(Collectors.summingInt(OrderDeliveryTypeTotalBo::getOrderNumber));
            for(OrderDeliveryTypeTotalBo orderDeliveryTypeTotalBo : deliveryTypeTotalBos){
                PieData pieData = new PieData();
                pieData.setTitle(orderDeliveryTypeTotalBo.getDeliveryType());
                pieData.setValue(orderDeliveryTypeTotalBo.getOrderNumber().doubleValue());
                pieData.setRate(ArithUtil.div(pieData.getValue(),count,2));
                pieData.setSmallTitle(String.format("%s个，%s",pieData.getValue().intValue(),new BigDecimal(ArithUtil.mul(pieData.getRate(),100)).intValue() + "%"));
                lineData.add(pieData);
            }
        }
        return lineData;
    }

    public List<PieData> initHaveDinnerUserTotalPie(List<OrderDeliveryTypeTotalBo> deliveryTypeTotalBos){
        List<PieData> lineData = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(deliveryTypeTotalBos)){
            Integer count = deliveryTypeTotalBos.stream().collect(Collectors.summingInt(OrderDeliveryTypeTotalBo::getOrderNumber));
            for(OrderDeliveryTypeTotalBo orderDeliveryTypeTotalBo : deliveryTypeTotalBos){
                PieData pieData = new PieData();
                pieData.setTitle(String.format("%s人",orderDeliveryTypeTotalBo.getDeliveryType()));
                pieData.setValue(orderDeliveryTypeTotalBo.getOrderNumber().doubleValue());
                pieData.setRate(ArithUtil.div(pieData.getValue(),count,2));
                pieData.setSmallTitle(String.format("%s个，%s",pieData.getValue().intValue(),new BigDecimal(ArithUtil.mul(pieData.getRate(),100)).intValue() + "%"));
                lineData.add(pieData);
            }
        }
        return lineData;
    }

    @Override
    public Map<String, Object> dataBankAnalysis(UnifiedCodeQueryVo unifiedCodeQueryVo) {
        return null;
    }

    @Override
    public Map<String, Object> dataBankAnalysisFlow(UnifiedCodeQueryVo unifiedCodeQueryVo) {
        DataBankAnalysisFlowRpVo dataBankAnalysisFlowRpVo = new DataBankAnalysisFlowRpVo();
        if(userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()).equals(CDPUtils.superAccountId)) {
            dataBankAnalysisFlowRpVo = CDPSimDataUtils.getCdpAllBo().getDataBankAnalysisFlow();
            return ResultMapHelper.success(dataBankAnalysisFlowRpVo, ResultMsg.MSG_GET_SUCCESS);
        }
//        dataBankAnalysisFlowRpVo.getPageVisitors().getYesPageVisitors().addAll(init(1, DateTimeUtils.getNowTimeStamp()));
//        DataBankAnalysisFlowRpVo.ConversionRateData conversionRateData = new DataBankAnalysisFlowRpVo.ConversionRateData();
//        conversionRateData.setAccessConversionRate("50%");
//        conversionRateData.setOrderConversionRate("30%");
//        conversionRateData.setTurnoverConversionRate("100%");
//        dataBankAnalysisFlowRpVo.getConversionRate().setSevenConversionRate(conversionRateData);
//        dataBankAnalysisFlowRpVo.getSourcePassengerFlow().getSevenSourcePassengerFlow().addAll(initPie(2, DateTimeUtils.getNowTimeStamp(), new String[]{"好友分享",
//                "扫码进入", "活动链接"}));

        Date nowTime = new Date();
        String endTime = DateTimeUtils.converDateToString(nowTime, DateTimeUtils.DATE_PATTERN_DAY);
        CdpUtilsBo cdpUtilsBo = new CdpUtilsBo();
        //页面进入人次(昨日)
        String startTime = DateTimeUtils.converDateToString(DateTimeUtils.addDay(nowTime, -1), DateTimeUtils.DATE_PATTERN_DAY);
        dankCdpOrderTotalService.getYesDayData(cdpUtilsBo,unifiedCodeQueryVo.getUserId(),"accessUser");
        dataBankAnalysisFlowRpVo.getPageVisitors().setYesPageVisitors(initLineDataDesc(STAT_HOUR,DateTimeUtils.converDateToInterger(nowTime),cdpUtilsBo.getYesLineData()));
        //访问率
        CDPUtils.orderAccessConversionRate(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startTime, endTime);
        dataBankAnalysisFlowRpVo.getConversionRate().getYesConversionRate().setAccessConversionRate(ArithUtil.round(ArithUtil.mul(cdpUtilsBo.getAccessRate(), 100), 2) + "%");
        //下单转化率
        CDPUtils.orderPayOrderRate(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startTime, endTime);
        dataBankAnalysisFlowRpVo.getConversionRate().getYesConversionRate().setOrderConversionRate(ArithUtil.round(ArithUtil.mul(cdpUtilsBo.getPayOrderRate(), 100), 2) + "%");
        //成交转化率
        CDPUtils.orderPayConversionRate(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startTime, endTime);
        dataBankAnalysisFlowRpVo.getConversionRate().getYesConversionRate().setOrderConversionRate(ArithUtil.round(ArithUtil.mul(cdpUtilsBo.getPayRate(), 100), 2) + "%");


        //消费转化率(7天)
        //TODO 7天
        startTime = DateTimeUtils.converDateToString(DateTimeUtils.addDay(nowTime, -7), DateTimeUtils.DATE_PATTERN_DAY);
        //页面进入人次（7天）
        CDPUtils.userAccessUserTotalGroup(cdpUtilsBo,userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()),startTime,endTime,"day");
        dataBankAnalysisFlowRpVo.getPageVisitors().setSevenPageVisitors(initLineDataDesc(STAT_WEEK,DateTimeUtils.converDateToInterger(nowTime),cdpUtilsBo.getGroupLineData()));
        //访问率
        CDPUtils.orderAccessConversionRate(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startTime, endTime);
        dataBankAnalysisFlowRpVo.getConversionRate().getSevenConversionRate().setAccessConversionRate(ArithUtil.round(ArithUtil.mul(cdpUtilsBo.getAccessRate(), 100), 2) + "%");
        //下单转化率
        CDPUtils.orderPayOrderRate(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startTime, endTime);
        dataBankAnalysisFlowRpVo.getConversionRate().getSevenConversionRate().setOrderConversionRate(ArithUtil.round(ArithUtil.mul(cdpUtilsBo.getPayOrderRate(), 100), 2) + "%");
        //成交转化率
        CDPUtils.orderPayConversionRate(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startTime, endTime);
        dataBankAnalysisFlowRpVo.getConversionRate().getSevenConversionRate().setOrderConversionRate(ArithUtil.round(ArithUtil.mul(cdpUtilsBo.getPayRate(), 100), 2) + "%");

        //TODO 30天
        startTime = DateTimeUtils.converDateToString(DateTimeUtils.addDay(nowTime, -30), DateTimeUtils.DATE_PATTERN_DAY);
        //页面进入人次
        CDPUtils.userAccessUserTotalGroup(cdpUtilsBo,userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()),startTime,endTime,"day");
        dataBankAnalysisFlowRpVo.getPageVisitors().setThirtyPageVisitors(initLineDataDesc(STAT_THIRD,DateTimeUtils.converDateToInterger(nowTime),cdpUtilsBo.getGroupLineData()));
        //访问率
        CDPUtils.orderAccessConversionRate(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startTime, endTime);
        dataBankAnalysisFlowRpVo.getConversionRate().getThirtyConversionRate().setAccessConversionRate(ArithUtil.round(ArithUtil.mul(cdpUtilsBo.getAccessRate(), 100), 2) + "%");
        //下单转化率
        CDPUtils.orderPayOrderRate(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startTime, endTime);
        dataBankAnalysisFlowRpVo.getConversionRate().getThirtyConversionRate().setOrderConversionRate(ArithUtil.round(ArithUtil.mul(cdpUtilsBo.getPayOrderRate(), 100), 2) + "%");
        //成交转化率
        CDPUtils.orderPayConversionRate(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startTime, endTime);
        dataBankAnalysisFlowRpVo.getConversionRate().getThirtyConversionRate().setOrderConversionRate(ArithUtil.round(ArithUtil.mul(cdpUtilsBo.getPayRate(), 100), 2) + "%");

        startTime = DateTimeUtils.converDateToString(DateTimeUtils.addMonth(nowTime, -6), DateTimeUtils.DATE_PATTERN_DAY);
        //TODO 半年
        //页面进入人次
        CDPUtils.userAccessUserTotalGroup(cdpUtilsBo,userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()),startTime,endTime,"day");
        dataBankAnalysisFlowRpVo.getPageVisitors().setHalfYearPageVisitors(initLineDataDesc(STAT_MONTH,DateTimeUtils.converDateToInterger(nowTime),cdpUtilsBo.getGroupLineData()));
        //访问率
        CDPUtils.orderAccessConversionRate(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startTime, endTime);
        dataBankAnalysisFlowRpVo.getConversionRate().getHalfYearConversionRate().setAccessConversionRate(ArithUtil.round(ArithUtil.mul(cdpUtilsBo.getAccessRate(), 100), 2) + "%");
        //下单转化率
        CDPUtils.orderPayOrderRate(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startTime, endTime);
        dataBankAnalysisFlowRpVo.getConversionRate().getHalfYearConversionRate().setOrderConversionRate(ArithUtil.round(ArithUtil.mul(cdpUtilsBo.getPayOrderRate(), 100), 2) + "%");
        //成交转化率
        CDPUtils.orderPayConversionRate(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startTime, endTime);
        dataBankAnalysisFlowRpVo.getConversionRate().getHalfYearConversionRate().setOrderConversionRate(ArithUtil.round(ArithUtil.mul(cdpUtilsBo.getPayRate(), 100), 2) + "%");

        return ResultMapHelper.success(dataBankAnalysisFlowRpVo, ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    public Map<String, Object> dataBankAnalysisProduct(UnifiedCodeQueryVo unifiedCodeQueryVo) {
        DataBankAnalysisProductRpVo rpVo = new DataBankAnalysisProductRpVo();
        if(userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()).equals(CDPUtils.superAccountId)) {
            rpVo = CDPSimDataUtils.getCdpAllBo().getDataBankAnalysisProduct();
            return ResultMapHelper.success(rpVo, ResultMsg.MSG_GET_SUCCESS);
        }
        //商品销售排行
        DataBankAnalysisProductRpVo.CommoditySalesRanking rankData = new DataBankAnalysisProductRpVo.CommoditySalesRanking();
        List<String> columns = new ArrayList<>();
        columns.add("排序");
        columns.add("商品名称");
        columns.add("销售量");
        rankData.setColumns(columns);
        rankData.setTitle("商品销售排行");
        rpVo.setCommoditySalesRanking(rankData);
        //商品回购率排行
        DataBankAnalysisProductRpVo.CommodityRepurchaseRate pcrRateRankData = new DataBankAnalysisProductRpVo.CommodityRepurchaseRate();
        columns.clear();
        columns.add("排序");
        columns.add("商品名称");
        columns.add("销售量");
        columns.add("回购率");
        pcrRateRankData.setColumns(columns);
        pcrRateRankData.setTitle("商品回购率排行");
        rpVo.setCommodityRepurchaseRate(pcrRateRankData);
        //商品转化率排行
        DataBankAnalysisProductRpVo.CommodityConversionRate csRateRankData = new DataBankAnalysisProductRpVo.CommodityConversionRate();
        columns.clear();
        columns.add("排序");
        columns.add("商品名称");
        columns.add("销售量");
        columns.add("回购率");
        csRateRankData.setColumns(columns);
        csRateRankData.setTitle("商品转化率排行");
        rpVo.setCommodityConversionRate(csRateRankData);


        //initTable(rankData);

        Date nowTime = new Date();
        String endTime = DateTimeUtils.converDateToString(nowTime, DateTimeUtils.DATE_PATTERN_DAY);

        CdpUtilsBo cdpUtilsBo = new CdpUtilsBo();
        //昨日
        String startTime = DateTimeUtils.converDateToString(DateTimeUtils.addDay(nowTime, -1), DateTimeUtils.DATE_PATTERN_DAY);
        CDPUtils.productSaleTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startTime, endTime);
        rpVo.getCommoditySalesRanking().setYesRankingData(initSaleData(cdpUtilsBo.getProductSaleNum()));
        CDPUtils.productRepurchaseBuyRate(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), "7");
        rpVo.getCommodityRepurchaseRate().setYesRankingData(initSaleQuantityRateData(cdpUtilsBo.getProductSaleNumMap(), cdpUtilsBo.getProductQuantityRate()));
        rpVo.getCommodityConversionRate().setYesRankingData(initSaleData(cdpUtilsBo.getSuccessConvertRate()));
        //7天
        startTime = DateTimeUtils.converDateToString(DateTimeUtils.addDay(nowTime, -7), DateTimeUtils.DATE_PATTERN_DAY);
        CDPUtils.productSaleTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startTime, endTime);
        //处理数据
        rpVo.getCommoditySalesRanking().setSevenRankingData(initSaleData(cdpUtilsBo.getProductSaleNum()));
        //回购率
        CDPUtils.productRepurchaseBuyRate(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), "7");
        rpVo.getCommodityRepurchaseRate().setSevenRankingData(initSaleQuantityRateData(cdpUtilsBo.getProductSaleNumMap(), cdpUtilsBo.getProductQuantityRate()));
        rpVo.getCommodityConversionRate().setSevenRankingData(initConversionRateData(cdpUtilsBo.getSuccessConvertRate()));

        //30日
        startTime = DateTimeUtils.converDateToString(DateTimeUtils.addDay(nowTime, -30), DateTimeUtils.DATE_PATTERN_DAY);
        CDPUtils.productSaleTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startTime, endTime);
        rpVo.getCommoditySalesRanking().setThirdRankingData(initSaleData(cdpUtilsBo.getProductSaleNum()));

        CDPUtils.productRepurchaseBuyRate(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), "30");
        rpVo.getCommodityRepurchaseRate().setThirdRankingData(initSaleQuantityRateData(cdpUtilsBo.getProductSaleNumMap(), cdpUtilsBo.getProductQuantityRate()));

        rpVo.getCommodityConversionRate().setThirdRankingData(initConversionRateData(cdpUtilsBo.getSuccessConvertRate()));


        //半年
        startTime = DateTimeUtils.converDateToString(DateTimeUtils.addMonth(nowTime, -6), DateTimeUtils.DATE_PATTERN_DAY);
        CDPUtils.productSaleTotal(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), startTime, endTime);
        rpVo.getCommoditySalesRanking().setHalfYearRankingData(initSaleData(cdpUtilsBo.getProductSaleNum()));

        CDPUtils.productRepurchaseBuyRate(cdpUtilsBo, userService.getAccountIdByUserMap(unifiedCodeQueryVo.getUserId()), "180");
        rpVo.getCommodityRepurchaseRate().setHalfYearRankingData(initSaleQuantityRateData(cdpUtilsBo.getProductSaleNumMap(), cdpUtilsBo.getProductQuantityRate()));

        rpVo.getCommodityConversionRate().setHalfYearRankingData(initConversionRateData(cdpUtilsBo.getSuccessConvertRate()));


        if (CollectionUtils.isNotEmpty(rpVo.getCommoditySalesRanking().getThirdRankingData())) {
            rpVo.getCommoditySalesRanking().setCommoditySalesBusinessAdvice(String.format(rpVo.getCommoditySalesRanking().getCommoditySalesBusinessAdvice(), rpVo.getCommoditySalesRanking().getThirdRankingData().get(0).get(1)));
        } else {
            rpVo.getCommoditySalesRanking().setCommoditySalesBusinessAdvice("");
        }
        return ResultMapHelper.success(rpVo, ResultMsg.MSG_GET_SUCCESS);
    }

    private List<List<String>> initConversionRateData(List<SaleGoodTableDataBo> saleSuccessConversionRate) {
        SaleGoodTableDataBo.sortConversionRate(saleSuccessConversionRate);
        List<List<String>> tableData = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            List<String> data = new ArrayList<>();
            if(i < saleSuccessConversionRate.size()) {
                data.add(String.valueOf(i + 1));
                data.add(saleSuccessConversionRate.get(i).getProductName());
                data.add(saleSuccessConversionRate.get(i).getSuccessConversionRate().toString() + "%");
            }else {
                data.add(String.valueOf(i + 1));
                data.add("--");;
                data.add("--");
            }
            tableData.add(data);
        }
        return tableData;
    }

    private void initTable(DataBankAnalysisProductRpVo.CommoditySalesRanking rankData) {
        List<List<String>> tableData = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            List<String> data = new ArrayList<>();
            data.add(String.valueOf(i));
            data.add("商品" + i);
            data.add(String.valueOf((int) (Math.random() * 10000)));
            tableData.add(data);
        }
        rankData.setSevenRankingData(tableData);
        rankData.setThirdRankingData(tableData);
        rankData.setHalfYearRankingData(tableData);
    }

    private List<List<String>> initSaleData(List<SaleGoodTableDataBo> saleGoodTableDataBos) {
        SaleGoodTableDataBo.sortSaleGood(saleGoodTableDataBos);
        List<List<String>> tableData = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            List<String> data = new ArrayList<>();
            if (i < saleGoodTableDataBos.size()) {
                data.add(String.valueOf(i + 1));
                data.add(saleGoodTableDataBos.get(i).getProductName());
                data.add(saleGoodTableDataBos.get(i).getProductQuantity().toString());
                tableData.add(data);
            }else {
                data.add(String.valueOf(i + 1));
                data.add("-");
                data.add("-");
                tableData.add(data);
            }
        }
        return tableData;
    }


    private List<List<String>> initSaleQuantityRateData(Map<String, Integer> productNumMap, List<SaleGoodTableDataBo> saleGoodTableDataBos) {
        SaleGoodTableDataBo.sortProductQuantityRate(saleGoodTableDataBos);
        List<List<String>> tableData = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            List<String> data = new ArrayList<>();
            if(i < saleGoodTableDataBos.size()) {
                data.add(String.valueOf(i + 1));
                data.add(saleGoodTableDataBos.get(i).getProductName());
                if (productNumMap.containsKey(saleGoodTableDataBos.get(i).getProductId())) {
                    data.add(productNumMap.get(saleGoodTableDataBos.get(i).getProductId()).toString());
                } else {
                    data.add("--");
                }
                data.add(saleGoodTableDataBos.get(i).getProductQuantityRate().toString() + "%");
            }else {
                data.add(String.valueOf(i + 1));
                data.add("--");
                data.add("--");
                data.add("--");
            }
            tableData.add(data);
        }
        return tableData;
    }

    @Override
    public Map<String, Object> getBusiness(DataBankBusinessVo dataBankBusinessVo) {
        GetBusinessRpVo getBusinessRpVo = new GetBusinessRpVo();
        DxshAppIndustry dxshAppIndustry = dxshAppIndustryChooseService.getUserChooseInIndustry(dataBankBusinessVo.getUserId());
        if (dxshAppIndustry != null) {
            getBusinessRpVo.setIndustryName(dxshAppIndustry.getIndustryName());
        }
        //调用到一次新的渠道
        Map<String,Object> homePageResultMap = this.managementHomePage_v2(dataBankBusinessVo);
        if(ResultMapHelper.isSuccess(homePageResultMap)){
            ManagementHomePageRpVo_v2 homePageRpVo_v2 = (ManagementHomePageRpVo_v2) homePageResultMap.get("data");
            getBusinessRpVo.setNewChannelModel(homePageRpVo_v2.getChannels());
        }
        if (dataBankBusinessVo.getBusinessId().equals(Constants.COMMON_FLAG_FALSE_INT)) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("business", getBusinessRpVo);
            return ResultMapHelper.success(resultMap, ResultMsg.MSG_GET_SUCCESS);
        }
        BankBusinessBo bankBusinessBo = this.getChooseBusinessData(dataBankBusinessVo.getBusinessId());
        if (bankBusinessBo == null) {
            return ResultMapHelper.result(ResultCode.CODE_ERROR_100, ResultMsg.HAND_FAIL);
        }
        BeanUtils.copyProperties(bankBusinessBo, getBusinessRpVo);
        if (getBusinessRpVo.getIndustryName().equals("未知行业")) {
            if (StringUtils.isNotBlank(getBusinessRpVo.getTypeName())) {
                if (BankBusinessController.industryTypeMap.containsKey(getBusinessRpVo.getTypeName())) {
                    getBusinessRpVo.setIndustryName(BankBusinessController.industryTypeMap.get(getBusinessRpVo.getTypeName()));
                }
            }
        }
        Map<String, Object> retMap = dxshAppUserChannelService.getMyStore(dataBankBusinessVo.getUserId(), dataBankBusinessVo.getBusinessId());
        if (ResultMapHelper.isSuccess(retMap)) {
            GetMyStoreRpVo getMyStoreRpVo = (GetMyStoreRpVo) retMap.get("data");
            if (CollectionUtils.isNotEmpty(getMyStoreRpVo.getStores())) {
                GetMyStoreRpVo.StoresVo storesVo = getMyStoreRpVo.getStores().get(0);
                List<GetMyStoreRpVo.ChannelVo> channelVos = storesVo.getChannels();
                if (CollectionUtils.isNotEmpty(channelVos)) {
                    for (GetMyStoreRpVo.ChannelVo channelVo : channelVos) {
                        ManagementHomePageRpVo.ManagementHomePageChannels managementHomePageChannels = new ManagementHomePageRpVo.ManagementHomePageChannels();
                        BeanUtils.copyProperties(channelVo, managementHomePageChannels);
                        if (channelVo.getChannel() == DxshAppUserChannelService.CHANNEL_MDD) {
                            managementHomePageChannels.setChannelName(bankBusinessBo.getVendorName());
                        } else if (channelVo.getChannel() == DxshAppUserChannelService.CHANNEL_MINAPP) {
                            managementHomePageChannels.setChannelName(managementHomePageChannels.getMinAppName());
                        }
                        getBusinessRpVo.getChannels().add(managementHomePageChannels);
                    }
                }
            }
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("business", getBusinessRpVo);
        return ResultMapHelper.success(resultMap, ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    public Map<String, Object> managementHomePage_v2(DataBankBusinessVo dataBankBusinessVo) {
        ManagementHomePageRpVo_v2 managementHomePageRpVo = new ManagementHomePageRpVo_v2();
        //上面的几种渠道
        Map<String,Object> retMap = dxshAppUserChannelService.getMyAllChannel(dataBankBusinessVo.getUserId(),0,0);
        if(ResultMapHelper.isSuccess(retMap)){
            GetMyAllChannelRpVo getMyStoreRpVo = (GetMyAllChannelRpVo) retMap.get("data");
            if(CollectionUtils.isNotEmpty(getMyStoreRpVo.getChannels())){
                for(GetMyAllChannelRpVo.StoresVo storesVo : getMyStoreRpVo.getChannels()){
                    ManagementHomePageRpVo_v2.ManagementHomePageChannels managementHomePageChannels = new ManagementHomePageRpVo_v2.ManagementHomePageChannels();
                    BeanUtils.copyProperties(storesVo,managementHomePageChannels);
                    managementHomePageRpVo.getChannels().add(managementHomePageChannels);
                }
            }
        }
        return ResultMapHelper.success(managementHomePageRpVo,ResultMsg.MSG_GET_SUCCESS);
    }
}
